package com.weijian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weijian.dal.entity.MatchDetail;
import com.weijian.dal.entity.MatchResult;
import com.weijian.dto.match.MatchResultData;
import com.weijian.dto.match.RouteShipmentDTO;
import com.weijian.mapper.MatchResultMapper;
import com.weijian.service.MatchDetailService;
import com.weijian.service.MatchResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 匹配结果服务实现类
 */
@Service
public class MatchResultServiceImpl extends ServiceImpl<MatchResultMapper, MatchResult> implements MatchResultService {

    @Autowired
    private MatchDetailService matchDetailService;

    @Override
    public List<MatchResultData> getAllMatchResultsWithDetails() {
        // 查询所有匹配结果
        List<MatchResult> matchResults = this.list();

        if (matchResults.isEmpty()) {
            return Collections.emptyList();
        }

        // 收集所有matchResultId
        List<Integer> matchResultIds = matchResults.stream()
                .map(MatchResult::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        // 批量查询所有匹配详情
        List<MatchDetail> allMatchDetails = matchDetailService.listByIds(matchResultIds);

        // 按matchResultId分组
        Map<Integer, List<MatchDetail>> detailsMap = allMatchDetails.stream()
                .collect(Collectors.groupingBy(MatchDetail::getMatchResultId));

        // 组装结果
        return matchResults.stream()
                .map(matchResult -> {
                    MatchResultData matchResultData = new MatchResultData();
                    matchResultData.setMatchResult(matchResult);
                    matchResultData.setMatchDetails(
                            detailsMap.getOrDefault(matchResult.getId(), Collections.emptyList())
                    );
                    return matchResultData;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<MatchResultData> getMatchResultsWithDetailsByDataId(Integer dataId) {
        // 查询指定dataId的匹配结果
        List<MatchResult> matchResults = this.lambdaQuery()
                .eq(MatchResult::getDataId, dataId)
                .list();

        if (matchResults.isEmpty()) {
            return Collections.emptyList();
        }

        // 收集所有matchResultId
        List<Integer> matchResultIds = matchResults.stream()
                .map(MatchResult::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        // 批量查询所有匹配详情
        List<MatchDetail> allMatchDetails = matchDetailService.lambdaQuery()
                .eq(MatchDetail::getDataId, dataId)
                .in(MatchDetail::getMatchResultId, matchResultIds)
                .list();

        // 按matchResultId分组
        Map<Integer, List<MatchDetail>> detailsMap = allMatchDetails.stream()
                .collect(Collectors.groupingBy(MatchDetail::getMatchResultId));

        // 组装结果
        return matchResults.stream()
                .map(matchResult -> {
                    MatchResultData matchResultData = new MatchResultData();
                    matchResultData.setMatchResult(matchResult);
                    matchResultData.setMatchDetails(
                            detailsMap.getOrDefault(matchResult.getId(), Collections.emptyList())
                    );
                    return matchResultData;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<MatchDetail> getMatchDetailsByRouteId(Integer routeId) {
        return matchDetailService.lambdaQuery().eq(MatchDetail::getRouteId, routeId).list();
    }

    @Override
    public MatchResult getMatchResultById(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<RouteShipmentDTO> getShipmentIdsByRouteIds(List<Integer> routeIds, Integer dataId) {
        if (routeIds == null || routeIds.isEmpty()) {
            return Collections.emptyList();
        }
        MatchResult matchResult = lambdaQuery().eq(dataId != null, MatchResult::getDataId, dataId).one();

        // 批量查询所有匹配详情
        List<MatchDetail> matchDetails = matchDetailService.lambdaQuery()
                .eq(MatchDetail::getMatchResultId, matchResult.getId())
                .in(MatchDetail::getRouteId, routeIds)
                .list();

        // 按routeId分组，并收集shipmentId
        Map<Integer, List<Integer>> routeShipmentMap = matchDetails.stream()
                .collect(Collectors.groupingBy(
                        MatchDetail::getRouteId,
                        Collectors.mapping(MatchDetail::getShipmentId, Collectors.toList())
                ));

        // 转换为RouteShipmentDTO列表
        return routeIds.stream()
                .map(routeId -> {
                    RouteShipmentDTO dto = new RouteShipmentDTO();
                    dto.setRouteId(routeId);
                    dto.setShipmentIds(routeShipmentMap.getOrDefault(routeId, Collections.emptyList()));
                    return dto;
                })
                .collect(Collectors.toList());
    }
}